import 'package:async/async.dart';
import 'package:boss/common/common.dart';
import 'package:boss/main.dart';
import 'package:boss/models/store_type.dart';
import 'package:boss/provider/login_user_provider.dart';
import 'package:boss/ui/page/login/constant/login_type.dart';
import 'package:boss/ui/page/pickerAddress/picker_address_page.dart';
import 'package:boss/ui/page/regist/page/completeMessage/complete_message_page.dart';
import 'package:boss/ui/widget/loading/loading.dart';
import 'package:flutter/material.dart';
import 'package:hi_common/hi_common.dart';
import 'package:provider/provider.dart';

class RegistProvider extends ChangeNotifier {
  String phone;
  String code;
  String password;
  String confirmPassword;
  List<StoreType> storeTypes = [];
  StoreType selectedStoreType;
  String storeName;
  String provinces;
  String provincesCode;
  String city;
  String cityCode;
  String address;
  String district;
  String districtCode;
  String houseNumber;
  double latitude;
  double longitude;
  bool isAgreeProtocol = false;

  String get addressText {
    return '${provinces ?? ''}${city ?? ''}${district ?? ''}${address ?? ''}${houseNumber ?? ''}';
  }

  setPhone(String value) {
    phone = value;
    notifyListeners();
  }

  setCode(String value) {
    code = value;
    notifyListeners();
  }

  setPassword(String value) {
    password = value;
    notifyListeners();
  }

  setConfirmPassword(String value) {
    confirmPassword = value;
    notifyListeners();
  }

  checkStore(BuildContext context) async {
    if (!RegUtil.testPhone(phone)) {
      toast('请输入正确的手机号');
      return;
    }
    if (!RegUtil.testCode(code)) {
      toast('请输入4位验证码');
      return;
    }
    if (!RegUtil.testPwd(password)) {
      toast('请输入6-16位含数字字母密码');
      return;
    }
    if (password != confirmPassword) {
      toast('密码不一致');
      return;
    }
    if (!isAgreeProtocol) {
      toast('必须同意服务协议和隐私档案');
      return;
    }

    try {
      showLoadingToast();

      var result = await api.storeCheckData({
        'username': phone,
        'verifyCode': code,
        'password': StringUtil.md5(password),
        'type': 1,
      });
      if (!result.successful()) {
        toast(result.msg);
        return;
      }
      Navigator.pushNamed(context, CompleteMessage.pageName);
    } catch (e) {
      toast(Constant.httpServeErrText);
    } finally {
      hideLoadingToast();
    }
  }

  Future<Result<List<StoreType>>> initStoreType() async {
    try {
      var response = await api.queryStoreType();
      if (!response.successful()) {
        return Result.error(response.msg);
      }

      storeTypes = response.data;
      notifyListeners();

      return Result.value(response.data);
    } catch (e) {
      return Result.error(e.toString());
    }
  }

  setStoreType(StoreType storeType) {
    selectedStoreType = storeType;
    notifyListeners();
  }

  setStoreName(String value) {
    storeName = value;
    notifyListeners();
  }

  setAddress(String value) {
    address = value;
    notifyListeners();
  }

  registStore(BuildContext context) async {
    if (storeName == null || storeName == '') {
      toast('请输入店铺名');
      return;
    }

    if (longitude == null || latitude == null) {
      toast('请选择店铺所在区域');
      return;
    }

    if (selectedStoreType == null) {
      toast('请选择餐厅类型');
      return;
    }

    try {
      showLoadingToast();

      var response = await api.addStore({
        'addressDetail': address,
        'city': city,
        'province': provinces,
        'district': district,
        'password': StringUtil.md5(password),
        'storeName': storeName,
        'storeTypeId': selectedStoreType.storeTypeId,
        'storeTypeName': selectedStoreType.storeTypeName,
        'verifyCode': code,
        'phone': phone,
        'districtCode': districtCode,
        'cityCode': cityCode,
        'provinceCode': provincesCode,
        'latitude': latitude,
        'longitude': longitude,
        'houseNumber': houseNumber,
      });

      if (!response.successful()) {
        toast(response.msg);
        return;
      }

      var loginUserProvider = Provider.of<LoginUserProvider>(context, listen: false);
      await loginUserProvider.login(
        context,
        loginType: LoginType.userPassword,
        storeNo: response.data.storeNo,
        phone: phone,
        password: password,
      );

      reset();
    } catch (e) {
      toast(e.toString());
    } finally {
      hideLoadingToast();
    }
  }

  reset() {
    phone = null;
    code = null;
    password = null;
    confirmPassword = null;
    storeTypes = [];
    selectedStoreType = null;
    storeName = null;
    provinces = null;
    provincesCode = null;
    city = null;
    cityCode = null;
    district = null;
    districtCode = null;
    houseNumber = null;
    latitude = null;
    longitude = null;
    address = null;
  }

  toggleAgreeProtocol() {
    isAgreeProtocol = !isAgreeProtocol;
    notifyListeners();
  }

  void setStoreAddress(PCDHData data) {
    provinces = data.provinces;
    provincesCode = data.provincesCode;
    city = data.city;
    cityCode = data.cityCode;
    district = data.district;
    districtCode = data.districtCode;
    address = data.address;
    houseNumber = data.houseNumber;
    latitude = data.latitude;
    longitude = data.longitude;
    notifyListeners();
  }
}
